//
//  OrderRecordTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/11/1.
//

import UIKit

class OrderRecordTableViewCell: UITableViewCell {
    var bgView: UIView!
    var logoImageView: UIImageView!
    var typeLab: UILabel!
    var stateLab: UILabel!
    var carImageView: UIImageView!
    var specialBtn: UIButton!
    var nameLab: UILabel!
    var priceLab: UILabel!
    var timeLab: UILabel!
    var handselPriceLab: UILabel!//定金
    var payBtn: UIButton!
    var model: OrderContentListModel!//model
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = Theme.viewBkgGrayColor
        self.clipsToBounds = true
        self.initUI()
    }
    
    func initUI(){
       
        self.bgView = UIView(frame: .zero)
        self.bgView.backgroundColor = .white
        self.bgView.layer.cornerRadius = 8
        self.bgView.layer.masksToBounds = true
        self.contentView.addSubview(self.bgView)
        
        self.logoImageView = UIImageView(frame: .zero)
        self.bgView.addSubview(self.logoImageView)
        
        self.typeLab = UILabel(frame: .zero)
        self.typeLab.textColor = Theme.titleLabColor
        self.typeLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.bgView.addSubview(self.typeLab)
        
      
        
        self.stateLab = UILabel(frame: .zero)
        self.stateLab.textColor = Theme.redColor
        self.stateLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.bgView.addSubview(self.stateLab)
        
        
        self.carImageView = UIImageView(frame: .zero)
        self.carImageView.contentMode = .scaleAspectFill
        self.carImageView.clipsToBounds = true
//        self.carImageView.layer.cornerRadius = 8
//        self.carImageView.layer.masksToBounds = true
        self.bgView.addSubview(self.carImageView)
        
        self.specialBtn = UIButton(frame: .zero)
        var tempImg = UIImage(named: "icon_special")!
        tempImg = tempImg.stretchableImage(withLeftCapWidth: Int(tempImg.size.width)/2, topCapHeight: Int(tempImg.size.height))
        self.specialBtn.setup_backgroundImage(image: tempImg)
        self.specialBtn.setTitleColor(.white, for: .normal)
        self.specialBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.contentView.addSubview(self.specialBtn)
        self.specialBtn.setTitle(MMLocalizedString("special", comment: "特价"), for: .normal)
        
       
        
        self.nameLab = UILabel(frame: .zero)
        self.nameLab.textColor = Theme.titleLabColor
        self.nameLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.nameLab.numberOfLines = 0
        self.bgView.addSubview(self.nameLab)
        
        
        self.priceLab = UILabel(frame: .zero)
        self.priceLab.textColor = Theme.titleLabColor
        self.priceLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.bgView.addSubview(self.priceLab)
        

        
        
        self.timeLab = UILabel(frame: .zero)
        self.timeLab.textColor = UIColor(hex: 0x939AA8)
        self.timeLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.bgView.addSubview(self.timeLab)
        
        
        self.handselPriceLab = UILabel(frame: .zero)
        self.handselPriceLab.textColor = Theme.titleLabColor
        self.handselPriceLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        self.bgView.addSubview(self.handselPriceLab)
        
        
        self.payBtn = UIButton.init(frame: .zero)
        self.payBtn.backgroundColor = Theme.mainColor
        self.payBtn.layer.cornerRadius = 8
        self.payBtn.layer.masksToBounds = true
        self.payBtn.setTitle(MMLocalizedString("pay_now", comment: "立即支付"), for: .normal)
        self.payBtn.setTitleColor(.white, for: .normal)
        self.payBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCMedium, size: 14)
        self.payBtn.addTarget(self, action: #selector(payBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.payBtn)
        
        self.logoImageView.image = UIImage(named: "icon_order_car")
//        self.typeLab.text = MMLocalizedString("used_car", comment: "二手车")
//        self.stateLab.text = "待支付"
//        self.carImageView.backgroundColor = Theme.mainColor
//        self.nameLab.text = "Volkswagen 2023 e-Golf 330Volkswagen 2023 e-Golf 330"
//        self.priceLab.text = "HK$1,688,000"
//        self.timeLab.text = "2023-09-11 17:35:25"
//        self.handselPriceLab.text = "定金 HK$1,688,000"
        
        self.addLayout()
        
    }
    
    func addLayout(){
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(9)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(197)
            make.bottom.equalTo(0)
        }
        
        self.logoImageView.snp.makeConstraints { make in
            make.top.equalTo(21)
            make.left.equalTo(14)
            make.width.equalTo(19)
            make.height.equalTo(20)
        }
        
        self.typeLab.snp.makeConstraints { make in
            make.top.equalTo(21)
            make.left.equalTo(self.logoImageView.snp.right).offset(4)
            make.height.equalTo(20)
        }
        
        
        self.stateLab.snp.makeConstraints { make in
            make.top.equalTo(22)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        self.carImageView.snp.makeConstraints { make in
            make.top.equalTo(self.logoImageView.snp.bottom).offset(10)
            make.left.equalTo(14)
            make.width.equalTo(90)
            make.height.equalTo(68)
        }
        
        specialBtn.titleLabel?.sizeToFit()
        let tempW = specialBtn.titleLabel?.frame.size.width ?? 33
        self.specialBtn.snp.makeConstraints { make in
            make.top.equalTo(self.logoImageView.snp.bottom).offset(6)
            make.left.equalTo(108 - tempW)
            make.width.equalTo(tempW + 10)
            make.height.equalTo(18)
        }
        
        self.nameLab.snp.makeConstraints { make in
            make.top.equalTo(self.logoImageView.snp.bottom).offset(10)
            make.left.equalTo(self.carImageView.snp.right).offset(10)
            make.right.equalTo(-14)
        }
        
        self.priceLab.snp.makeConstraints { make in
            make.top.equalTo(self.nameLab.snp.bottom).offset(6)
            make.left.equalTo(self.carImageView.snp.right).offset(10)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        
        self.timeLab.snp.makeConstraints { make in
            make.top.equalTo(self.carImageView.snp.bottom).offset(12)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        self.handselPriceLab.snp.makeConstraints { make in
            make.top.equalTo(self.timeLab.snp.bottom).offset(10)
            make.right.equalTo(-14)
            make.height.equalTo(22)
        }
        
        self.payBtn.snp.makeConstraints { make in
            make.top.equalTo(self.handselPriceLab.snp.bottom).offset(6)
            make.right.equalTo(-10)
            make.width.equalTo(140)
            make.height.equalTo(40)
            make.bottom.equalTo(-20)
        }
        
    }
    
    
    //setModel
    func setModel(model: OrderContentListModel){
        self.model = model
        switch model.type {
            case 2://商城
                self.logoImageView.image = UIImage(named: "icon_order_store")
                self.typeLab.text = MMLocalizedString("store", comment: "商城")
                self.storeTypeDealData(model: model)
            case 3:
                //海外车
                self.logoImageView.image = UIImage(named: "icon_order_car")
                self.typeLab.text = MMLocalizedString("sea_car_title", comment: "海外车辆")
                self.carTypeDealData(model: model)
            default://车
                self.logoImageView.image = UIImage(named: "icon_order_car")
                self.typeLab.text = MMLocalizedString("used_car", comment: "二手车")
                self.carTypeDealData(model: model)
        }
        self.timeLab.text = model.orderTime ?? ""
        self.updateConstraints()
        
    }
    
    //商城
    func storeTypeDealData(model: OrderContentListModel){
        self.payBtn.snp.updateConstraints { make in
            make.height.equalTo(0)
        }
        self.stateLab.textColor = Theme.titleLabColor
        switch model.status {
        case 1:
            self.payBtn.snp.updateConstraints { make in
                make.height.equalTo(40)
            }
            self.stateLab.textColor = Theme.redColor
            self.stateLab.text = MMLocalizedString("unpaid", comment: "待支付")
        case 2:
            self.stateLab.text = MMLocalizedString("havePaid", comment: "已支付")
        case 3:
            self.stateLab.text = MMLocalizedString("finished", comment: "已完成")
        case 4:
            self.stateLab.text = MMLocalizedString("refunded", comment: "已退款")
        case 5:
            self.stateLab.text = MMLocalizedString("canceled", comment: "已取消")
        default:
            self.stateLab.text = ""
        }
        
        self.carImageView.sd_setImage(with: URL.init(string: model.urlPicture ?? ""))
        self.specialBtn.isHidden = false
        self.nameLab.text = model.carName ?? ""
        
        self.priceLab.text = "X" + (model.itemNum ?? "0")
        let price = XTManageCenter.showPriceInComma(source: (model.price ?? 0.00).cleanZero)
        self.handselPriceLab.text = XTManageCenter.currentSign() + MMLocalizedString("actually_paid", comment: "实付") + price
    }
    
    //车辆
    func carTypeDealData(model: OrderContentListModel){
       let price = XTManageCenter.showPriceInComma(source: (model.price ?? 0.00).cleanZero)
        self.priceLab.text = XTManageCenter.currentSign() + price
        self.stateLab.textColor = Theme.titleLabColor
        self.payBtn.snp.updateConstraints { make in
            make.height.equalTo(0)
        }
        let depositAmount = XTManageCenter.showPriceInComma(source: (model.depositAmount ?? 0.00).cleanZero)
//        if model.type == 3 {
//            //海外车
//            self.handselPriceLab.isHidden = true
//            self.handselPriceLab.snp.updateConstraints { make in
//                make.height.equalTo(0)
//            }
//        }else{
//            self.handselPriceLab.isHidden = false
//            self.handselPriceLab.snp.updateConstraints { make in
//                make.height.equalTo(22)
//            }
//        }
        switch model.status {
            case 1:
                self.payBtn.snp.updateConstraints { make in
                    make.height.equalTo(40)
                }
                self.stateLab.textColor = Theme.redColor
                self.stateLab.text = MMLocalizedString("unpaid", comment: "待支付")
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"), XTManageCenter.currentSign(),depositAmount)
        case 2:
                if model.subscribeType ?? -1 == 0 {
                    //表示订购中
                    self.stateLab.text = MMLocalizedString("reserved_ing", comment: "订购中")
                }else{
                    self.stateLab.text = MMLocalizedString("reserved", comment: "已预订")
                }
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"), XTManageCenter.currentSign(),depositAmount)
            case 3:
                self.stateLab.text = MMLocalizedString("finished", comment: "已完成")
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"),
                                                   XTManageCenter.currentSign(),
                                                   depositAmount)
            case 4:
                self.stateLab.text = MMLocalizedString("unsubscribed", comment: "已退订")
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"),
                                                   XTManageCenter.currentSign(),
                                                   depositAmount)
            case 5:
                self.stateLab.text = MMLocalizedString("canceled", comment: "已取消")
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"),
                                                   XTManageCenter.currentSign(),
                                                   depositAmount)
            default:
                self.stateLab.text = ""
                self.handselPriceLab.text = ""
        }
    
        
        self.carImageView.sd_setImage(with: URL.init(string: model.backgroundImage ?? ""))
        self.nameLab.text = model.carName ?? ""
    }
    
    
    
    //MARK: - Antion
    @objc func payBtnAntion(){
        let vc = PaymentViewController()
        vc.pageType = 1
        vc.orderContentModel = self.model
        vc.modalPresentationStyle = .overFullScreen
        self.currentViewController()?.present(vc, animated: false)
        
        vc.pushFirstDataBlock = {[weak self] urlStr in
            let vc = MyWebController()
            vc.urlStr = urlStr
            vc.titleStr = MMLocalizedString("confirm_first_pay", comment: "FirstData支付")
            self?.currentViewController()?.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
